package hlhj.fhp.supreme.activitys

import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.WindowManager
import cn.sharesdk.framework.Platform
import cn.sharesdk.framework.PlatformActionListener
import cn.sharesdk.framework.ShareSDK
import cn.sharesdk.tencent.qq.QQ
import cn.sharesdk.wechat.friends.Wechat
import com.bumptech.glide.Glide
import com.google.gson.Gson
import com.hyphenate.EMCallBack
import com.hyphenate.chat.EMClient
import com.lzy.okgo.OkGo
import com.lzy.okgo.callback.StringCallback
import com.lzy.okgo.model.Response
import com.lzy.okgo.request.base.Request
import fhp.hlhj.giantfold.utils.LogUtil
import hlhj.fhp.supreme.BaseAty
import hlhj.fhp.supreme.Contents
import hlhj.fhp.supreme.MainActivity
import hlhj.fhp.supreme.R
import hlhj.fhp.supreme.javabean.*
import hlhj.fhp.supreme.network.Urls
import hlhj.fhp.supreme.utils.JsonCallBack
import hlhj.fhp.supreme.utils.MyUtils
import hlhj.fhp.supreme.utils.SPHelper
import kotlinx.android.synthetic.main.activity_login_aty.*
import java.util.HashMap

class LoginAty : BaseAty() {
    private lateinit var wx:Platform
    private lateinit var qq:Platform
    override fun getContentId(): Int = R.layout.activity_login_aty
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN)
    }
    override fun initView() {
        InitMob()
        LogUtil.log("环信？？")
        if (SPHelper.getLoginStatus(this)){
            OkGo.post<RegisBean>(Urls.BaseUrl+Urls.Login)
                    .params("mobile",SPHelper.getUserName(this))
                    .params("user_pass",SPHelper.getPwd(this))
                    .execute(object :JsonCallBack<RegisBean>(this){
                        override fun onSuccess(response: Response<RegisBean>?) {
//                            loadingDialog.dismiss()
                            val bean = response?.body()
                            if (bean?.code==1){
                                Contents.TOKEN=bean?.data.token
                                SPHelper.saveToken(this@LoginAty,bean.data.token)
                                val intent = Intent(this@LoginAty, MainActivity::class.java)
                                startActivity(intent)
                                finish()
                            }else{
                                MyUtils.toast(bean?.msg)
                            }
                        }
                    })
        }
    }

    private fun InitMob() {
        /*微信*/
        wx = ShareSDK.getPlatform(Wechat.NAME)
        wx.platformActionListener = object : PlatformActionListener {
            override fun onComplete(p0: Platform?, p1: Int, p2: HashMap<String, Any>?) {
                val wxBean = Gson().fromJson(p0?.db?.exportData(), WxBean::class.java)
                OkGo.post<BaseBean>(Urls.BaseUrl+Urls.THIREDLOGIN)
                        .params("type",1)
                        .params("openid",wxBean.openid)
                        .execute(object :JsonCallBack<BaseBean>(this@LoginAty){
                            override fun onSuccess(response: Response<BaseBean>?) {
                                val bean = response?.body()
                                LogUtil.log("准备跳转？？？？？")
                                if (bean?.code==300){
                                    LogUtil.log("准备跳转？？？？？")
                                    val intent = Intent(this@LoginAty, RegisStudentAty::class.java)
                                    intent.putExtra("et","2")
                                    intent.putExtra("type","1")
                                    intent.putExtra("openid",wxBean.openid)
                                    startActivity(intent)
                                }else if (bean?.code==1){
                                    val gson = Gson()
                                    val loginBean = gson.fromJson(gson.toJson(bean), ThirdLoginBean::class.java)
                                    SPHelper.saveToken(this@LoginAty,loginBean?.data?.token!!)
                                    startActivity(Intent(this@LoginAty,MainActivity::class.java))
                                    finish()
                                }
                            }
                        })
            }

            override fun onCancel(p0: Platform?, p1: Int) {
            }

            override fun onError(p0: Platform?, p1: Int, p2: Throwable?) {
            }
        }
        /*QQ*/
        qq = ShareSDK.getPlatform(QQ.NAME)
        qq.platformActionListener=object :PlatformActionListener{
            override fun onComplete(p0: Platform?, p1: Int, p2: HashMap<String, Any>?) {
                LogUtil.e("fhp",p0?.db!!.exportData())
                val qqBean = Gson().fromJson(p0?.db.exportData(), QQBean::class.java)
                OkGo.post<BaseBean>(Urls.BaseUrl+Urls.THIREDLOGIN)
                        .params("type",2)
                        .params("openid",qqBean.userID)
                        .execute(object :JsonCallBack<BaseBean>(this@LoginAty){
                            override fun onSuccess(response: Response<BaseBean>?) {
                                val bean = response?.body()
                                LogUtil.log("准备跳转？？？？？")
                                if (bean?.code==300){
                                    LogUtil.log("准备跳转？？？？？")
                                    val intent = Intent(this@LoginAty, RegisStudentAty::class.java)
                                    intent.putExtra("et","2")
                                    intent.putExtra("type","2")
                                    intent.putExtra("openid",qqBean.userID)
                                    startActivity(intent)
                                }else if (bean?.code==1){
                                    val gson = Gson()
                                    val loginBean = gson.fromJson(gson.toJson(bean), ThirdLoginBean::class.java)
                                    SPHelper.saveToken(this@LoginAty,loginBean?.data?.token!!)
                                    startActivity(Intent(this@LoginAty,MainActivity::class.java))
                                    finish()
                                }
                            }
                        })
            }

            override fun onCancel(p0: Platform?, p1: Int) {
            }

            override fun onError(p0: Platform?, p1: Int, p2: Throwable?) {
            }
        }
    }

    override fun initListener() {
        btRegis.setOnClickListener {
            val intent = Intent(this, RegisStudentAty::class.java)
            intent.putExtra("et","1")
            startActivity(intent)
        }

        btLogin.setOnClickListener {
            if (etPhone.text.toString().length==0||etPwd.text.toString().length==0){
                if (etPhone.text.isEmpty()){
                    MyUtils.toast("手机号未填写")
                    return@setOnClickListener
                }
                if (etPwd.text.isEmpty()){
                    MyUtils.toast("密码未填写")
                    return@setOnClickListener
                }
            }else{
                OkGo.post<RegisBean>(Urls.BaseUrl+Urls.Login)
                        .params("mobile",etPhone.text.toString())
                        .params("user_pass",etPwd.text.toString())
                        .execute(object :JsonCallBack<RegisBean>(){
                            override fun onSuccess(response: Response<RegisBean>?) {
                                val bean = response?.body()
                                if (bean?.code==1){
                                    Contents.TOKEN=bean?.data.token
                                    SPHelper.saveToken(this@LoginAty,bean.data.token)
                                    SPHelper.saveLoginStatus(this@LoginAty,true)
                                    SPHelper.saveUserInfo(this@LoginAty,etPhone.text.toString(),etPwd.text.toString())
                                    val intent = Intent(this@LoginAty, MainActivity::class.java)
                                    startActivity(intent)
                                    MyUtils.toast("登录成功")
                                    finish()
                                }else{
                                    MyUtils.toast(bean?.msg)
                                }
                            }
                        })
            }
        }
        btForget.setOnClickListener {
            val intent = Intent(this, ForgetAty::class.java)
            startActivity(intent)
        }

        btWx.setOnClickListener {
            wx.authorize()
        }
        btQQ.setOnClickListener {
            qq.authorize()
        }
        
    }
}
